package android.meetingatsb

import android.content.Intent
import android.meetingatsb.common.User
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import android.widget.Button
import android.widget.EditText
import android.widget.TextView


class UserInfoActivity : AppCompatActivity() {
    var user: User? = null

    lateinit var gender: TextView
    lateinit var age: TextView
    lateinit var rating: TextView

    lateinit var name: EditText
    lateinit var group: EditText
    lateinit var university: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_info)
        getUser()
        bindWidgets()
        setData()
        setListener()
    }

    private fun getUser() {
        user = User.getCurrUser(this)
        if (user == null) {
            intent = Intent(this, LoginActivity::class.java)
            startActivity(intent)
        }
    }

    private fun bindWidgets() {

        gender = findViewById(R.id.user_gender)
        age = findViewById(R.id.user_age)
        rating = findViewById(R.id.rating)

        name = findViewById(R.id.user_name)
        group = findViewById(R.id.user_group)
        university = findViewById(R.id.user_university)
    }

    private fun setData() {
        gender.text = user?.gender
        age.text = user?.age.toString()
        rating.text = user?.rating.toString()

        name.hint = user?.name
        group.hint = user?.group
        university.hint = user?.university
    }

    private fun setListener() {

        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        findViewById<Button>(R.id.submit_changed_info).setOnClickListener {
            val user = User(name.toString(), group.toString(), university.toString(), 21,"男",5F)
            User.setCurrUser(this, user)
            intent.setClass(this, MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
            startActivity(intent)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                intent.setClass(this, MainActivity::class.java)
                startActivity(intent)
            }
        }
        return super.onOptionsItemSelected(item)
    }

}